package com.dlyk.service.impl;

import com.dlyk.constant.Constants;
import com.dlyk.mapper.TProductMapper;
import com.dlyk.model.TClue;
import com.dlyk.model.TProduct;
import com.dlyk.model.TUser;
import com.dlyk.query.BaseQuery;
import com.dlyk.query.ProductQuery;
import com.dlyk.service.ProductService;
import com.dlyk.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/26 22:22:48
 * @version: 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private TProductMapper tProductMapper;

    @Override
    public List<TProduct> getAllOnSaleProduct() {
        return tProductMapper.selectAllOnSaleProduct();
    }

    @Override
    public PageInfo<TProduct> getProductByPage(Integer current,ProductQuery productQuery) {
        //设置PageHelper的分页参数
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TProduct> list = tProductMapper.selectProductByPage(productQuery);
        //封装分页参数到PageInfo
        PageInfo<TProduct> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TProduct getProductById(Integer id) {
        return tProductMapper.selectDetailById(id);
    }

    @Override
    public int saveProduct(ProductQuery productQuery) {
        TProduct tProduct = new TProduct();

        BeanUtils.copyProperties(productQuery,tProduct);

        tProduct.setCreateTime(new Date());//创建时间

        ///登陆人的id
        Integer loginUserId = JWTUtils.parseJWTByUserId(productQuery.getToken());
        tProduct.setCreateBy(loginUserId);//创建人
        return tProductMapper.insertSelective(tProduct);
    }

    @Override
    public int updateProduct(ProductQuery productQuery) {
        TProduct tProduct = new TProduct();

        BeanUtils.copyProperties(productQuery,tProduct);

        tProduct.setEditTime(new Date());//编辑时间

        ///登陆人的id
        Integer loginUserId = JWTUtils.parseJWTByUserId(productQuery.getToken());
        tProduct.setEditBy(loginUserId);//编辑人
        return tProductMapper.updateByPrimaryKeySelective(tProduct);
    }

    @Override
    public int delProductById(Integer id) {
        return tProductMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchDelProductByIds(List<String> idList) {
        return tProductMapper.deleteByIds(idList);
    }
}
